package com.jy.adscm.operation.xjfile.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jy.adscm.bean.system.JyMenu;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.xjfile.bean.XjFile;
import com.jy.adscm.operation.xjfile.mapper.mysql.XjFileMapper;
import com.jy.adscm.operation.xjfile.service.XjFileService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2019-05-07
 */
@Service
public class XjFileServiceImpl extends ServiceImpl<XjFileMapper, XjFile> implements XjFileService {
    @Autowired
    JyUserMapper jyUserMapper;
    @Cacheable(value = "xjfiletree")
    @Override
    public List<XjFile> selectAll() throws AdscmSystemException {
        Wrapper<XjFile> wrapper=new EntityWrapper<>();
        wrapper.eq("parentId",0);
        wrapper.setSqlSelect("id","parentId","name","code","rank");
        wrapper.orderBy("rank",false);
        List<XjFile> xjFiles = super.selectList(wrapper);
        if (xjFiles!=null&&xjFiles.size()>0){
            getAllChildren(xjFiles);
        }
        return xjFiles;
    }

    @Override
    public XjFile selectXjFile(Integer id) {

        XjFile xjFile = super.selectById(id);
        if (AdscmBaseUtils.getInstance().isNotEmpty(xjFile.getCreator(),null)){
            JyUser jyUser = jyUserMapper.selectById(xjFile.getCreator());
            xjFile.setCreatorName(jyUser.getName());
        }
        return xjFile;
    }

    public  void getAllChildren(  List<XjFile> xjFiles){
        for (XjFile xjFile : xjFiles) {
            Wrapper<XjFile> wrapper =new EntityWrapper<>();
            wrapper.eq("parentId",xjFile.getId());
            wrapper.setSqlSelect("id","parentId","name","code","rank");
            wrapper.orderBy("rank",false);
            List<XjFile> childrens = super.selectList(wrapper);
            if (childrens!=null &&childrens.size()>0){
                getAllChildren(childrens);
            }
            xjFile.setChlidren(childrens);
        }
    }


}
